<?php

class AdvController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
    	 public function filters()
	{
		return array(
			'accessControl',
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',
			      'actions' => array('login'),
			      'users' => array('*'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('add','index','edit','delete','category','add_cat','edit_cat','delete_cat'),
				'roles'=>array('admin'),
			),
			array('deny',
			      'users' => array('*'),
			),
		);
	}

	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	public function actionIndex()
	{
        if(isset($_POST['article-grid_c0'])){
           $this->deleteAll($_POST['article-grid_c0']);
        }
		$this->pagetitle = 'Статьи - '.Yii::app()->name;
		$this->render('index', array('model'=>Article::model()));
	}

   public function actionCategory()
   {
       $cid = 1;
       if(isset($_GET['pid']))
       {
           $cid = $_GET['pid'];
       }
       $this->pagetitle = 'Подразделы объявлений - '.Yii::app()->name;
       $this->render('index_cat', array('model'=>AdvTypeCategory::model(),'cid'=>$cid));
   }

    public function actionAdd_Cat()
    {
        $this->pagetitle = 'Подразделы объявлений - Добавление подраздела '.Yii::app()->name;
        $model = new AdvTypeCategory;
        if(isset($_POST['AdvTypeCategory']))
        {
            $model->attributes = $_POST['AdvTypeCategory'];
            if($model->save())
            {
                $this->redirect(Yii::app()->createUrl('adv/category'));
            }
        }
        $this->render('add_cat', array('model'=>$model));
    }

    public function actionEdit_Cat($id)
    {
        $this->pagetitle = 'Подразделы объявлений - Добавление подраздела '.Yii::app()->name;
        $model = AdvTypeCategory::model()->findByPk($id);
        if(isset($_POST['AdvTypeCategory']))
        {
            $model->attributes = $_POST['AdvTypeCategory'];
            if($model->save())
            {
                $this->redirect(Yii::app()->createUrl('adv/category'));
            }
        }
        $this->render('edit_cat', array('model'=>$model));
    }

    public function actionDelete_Cat($id)
    {
        AdvTypeCategory::model()->deleteByPk($id);
        $this->redirect(Yii::app()->createUrl('adv/category'));
    }




}